package org.hamster.common.components.pagination.base
{
	import flash.events.MouseEvent;
	
	import org.hamster.common.components.base.ImageButton;
	import org.hamster.common.components.pagination.base.event.PaginationEvent;
	
	import spark.components.Group;
	import spark.layouts.HorizontalLayout;
	
	[Event(type="org.hamster.common.components.pagination.base.event.PaginationEvent", name="pageChanged")]
	[Event(type="org.hamster.common.components.pagination.base.event.PaginationEvent", name="pageSizeChanged")]
	
	[Style(name="upBtnSkin",type="*")]
	[Style(name="upBtnSkinDisabled",type="*")]
	[Style(name="upBtnSkinDown",type="*")]
	[Style(name="upBtnSkinOver",type="*")]
	[Style(name="downBtnSkin",type="*")]
	[Style(name="downBtnSkinDisabled",type="*")]
	[Style(name="downBtnSkinDown",type="*")]
	[Style(name="downBtnSkinOver",type="*")]
	
	public class BasePagination extends Group
	{
		private var _currentPage:int = -1;
		private var _currentPageChanged:Boolean = false;
		
		private var _pageSize:int = -1;
		private var _pageSizeChanged:Boolean = false;
		
		protected var _upPageButton:ImageButton;
		protected var _dnPageButton:ImageButton;
		
		public function set currentPage(value:int):void
		{
			if (_currentPage != value) {
				var oldIndex:int = _currentPage;
				_currentPage = value;
				var newIndex:int = _currentPage;
				_currentPageChanged = true;
				this.invalidateProperties();
				if (this.hasEventListener(PaginationEvent.PAGE_CHANGED)) {
					var disEvt:PaginationEvent = new PaginationEvent(PaginationEvent.PAGE_CHANGED);
					disEvt.oldIndex = oldIndex;
					disEvt.newIndex = newIndex;
					this.dispatchEvent(disEvt);
				}
			}
		}
		
		public function get currentPage():int
		{
			return _currentPage;
		}
		
		public function set pageSize(value:int):void
		{
			if (_pageSize != value) {
				var oldSize:int = _pageSize;
				_pageSize = value;
				var newSize:int = _pageSize;
				_pageSizeChanged = true;
				if (this.hasEventListener(PaginationEvent.PAGE_SIZE_CHANGED)) {
					var disEvt:PaginationEvent = new PaginationEvent(PaginationEvent.PAGE_CHANGED);
					disEvt.oldSize = oldSize;
					disEvt.newSize = newSize;
					this.dispatchEvent(disEvt);
				}
			}
		}
		
		public function get pageSize():int
		{
			return _pageSize;
		}
		
		override protected function createChildren():void
		{
			super.createChildren();
			
			var buttons:Array = this.createButtons();
			
			this._upPageButton = buttons[0];
			this._dnPageButton = buttons[1];
		}
		
		protected function createButtons():Array
		{
			var upButton:ImageButton = new ImageButton();
			upButton.setStyle('imageSkin',				getStyle('upBtnSkin'));
			upButton.setStyle('imageSkinDisabled',		getStyle('upBtnSkinDisabled'));
			upButton.setStyle('imageSkinDown',			getStyle('upBtnSkinDown'));
			upButton.setStyle('imageSkinOver',			getStyle('upBtnSkinOver'));
			
			var dnButton:ImageButton = new ImageButton();
			dnButton.setStyle('imageSkin',				getStyle('downBtnSkin'));
			dnButton.setStyle('imageSkinDisabled',		getStyle('downBtnSkinDisabled'));
			dnButton.setStyle('imageSkinDown',			getStyle('downBtnSkinDown'));
			dnButton.setStyle('imageSkinOver',			getStyle('downBtnSkinOver'));
			
			upButton.addEventListener(MouseEvent.CLICK, upButtonClickHandler);
			dnButton.addEventListener(MouseEvent.CLICK, dnButtonClickHandler);
			
			return [upButton, dnButton];
		}
		
		protected function upButtonClickHandler(event:MouseEvent):void
		{
			if (currentPage < pageSize - 1) {
				currentPage++;
			}
		}
		
		protected function dnButtonClickHandler(event:MouseEvent):void
		{
			if (currentPage > 0) {
				currentPage--;
			}
		}
		
		public function BasePagination()
		{
			super();
			
			this.layout = new HorizontalLayout();
		}
		
		override public function styleChanged(styleProp:String):void
		{
			super.styleChanged(styleProp);
			
			switch (styleProp) {
				case "upBtnSkin":
				case "upBtnSkinDisabled":
				case "upBtnSkinDown":
				case "upBtnSkinOver":
					_upPageButton.setStyle(styleProp.replace('upBtn','image'), this.getStyle(styleProp));
					break;
				case "downBtnSkin":
				case "downBtnSkinDisabled":
				case "downBtnSkinDown":
				case "downBtnSkinOver":
					_dnPageButton.setStyle(styleProp.replace('upBtn','image'), this.getStyle(styleProp));
					break;
			}
		}
		
		override protected function commitProperties():void
		{
			super.commitProperties();
			
			if (_currentPageChanged) {
				commitCurrentPage();
				_currentPageChanged = false;
			}
			
			if (_pageSizeChanged) {
				commitPageSize();
				_pageSizeChanged = false;
			}
		}
		
		protected function commitCurrentPage():void
		{
			this._dnPageButton.enabled = currentPage > 0;
			this._upPageButton.enabled = currentPage < pageSize - 1;
		}
		
		protected function commitPageSize():void
		{
			if (pageSize <= 1) {
				this._upPageButton.visible = false;
				this._dnPageButton.visible = false;
			} else {
				this._upPageButton.visible = true;
				this._dnPageButton.visible = true;
			}
			commitCurrentPage();
		}
		
	}
}